﻿using B.S.Data.MES.API.Applications.BaseData.Command.ChanXIanCommand;
using B.S.Data.MES.API.Dto.ChanxianDto;
using B.S.Domain.BasicData.Factory_modeling.Production_line_management;
using B.S.Domain.BasicData.Factory_modeling.shop_management;
using B.S.Infratrctrue;
using B.S.Requst;
using LinqToDB;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.ChanXIanCommandHandlers
{
    public class ListChanXianCommandHandlers : IRequestHandler<ListChanXianCommand, APIRequst<APIPageing<List<ChanXianDto>>>>
    {
        private readonly IBaseRepository<Production_line_management> proRepository;
        private readonly IBaseRepository<shop_management> shopRepository;

        public ListChanXianCommandHandlers(IBaseRepository<Production_line_management> ProRepository,IBaseRepository<shop_management> shopRepository)
        {
            proRepository = ProRepository;
            this.shopRepository = shopRepository;
        }

        public Task<APIRequst<APIPageing<List<ChanXianDto>>>> Handle(ListChanXianCommand request, CancellationToken cancellationToken)
        {
         
            var list = from a in proRepository.GetAll() join b in shopRepository.GetAll() on a.Workid equals b.Workid select new ChanXianDto() 
            {
                Workid = b.Workid,
                Workshopcoding = b.Workshopcoding,
                Workshopname = b.Workshopname,
                describe = b.describe,
                Descriptionofproductionline=a.Descriptionofproductionline,
                Nameofproductionline=a.Nameofproductionline,
                Productionlinenumber=a.Productionlinenumber,
                ProductionlinePK=a.ProductionlinePK,
                CreateName=a.CreateName,
                CreateTime=a.CreateTime,
                UpdateTime=a.UpdateTime,
            };

            var totalCount = list.Count();
            var total = (int)Math.Ceiling((1.0 * totalCount) / request.PageSize);
            list=list.OrderByDescending(x=>x.ProductionlinePK).Skip((request.Pageindex-1)*request.PageSize).Take(request.PageSize);
            APIPageing<List<ChanXianDto>> Page = new APIPageing<List<ChanXianDto>>();
            Page.Total = total;
            Page.TotalCount = totalCount;
            Page.Data = list.ToList();
            return Task.FromResult(new APIRequst<APIPageing<List<ChanXianDto>>>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString(),
                Data =Page
            });
            
        }
    }
}
